package com.ml.mall.bean.base;

import com.alibaba.fastjson.JSON;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.enums.RetStatusNum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

@ApiModel("API REST返回data")
public class ApiRestResult<T> implements Serializable {
    public static final String SUCCESS = "200";
    @ApiModelProperty("状态")
    private String code;
    @ApiModelProperty("消息")
    private String msg;
    @ApiModelProperty("返回内容")
    private T data;


    private ApiRestResult(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ApiRestResult<T> data(T data) {
        this.data = data;
        return this;
    }
    public ApiRestResult<T> ApiRestResult(String code,T data) {
        this.data = data;
        this.code=code;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public String getCode() {
        return StringUtils.isBlank(this.code) ? String.valueOf(HttpStatus.OK.value()) : this.code;
    }

    public ApiRestResult<T> code(String code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return StringUtils.isBlank(this.msg) ? "操作成功" : this.msg;
    }

    public ApiRestResult<T> msg(String msg) {
        this.msg = msg;
        return this;
    }
    public static <T> ApiRestResult<T> apiSynBack(String code, String msg,T data) {
        return new ApiRestResult(code,msg,data);
    }
    public static <T> ApiRestResult<T> ok(T data) {
        return new ApiRestResult("200", "操作成功", data);
    }

    public static <T> ApiRestResult<T> ok() {
        return new ApiRestResult("200", "操作成功", (Object)null);
    }

    public static <T> ApiRestResult<T> err() {
        return new ApiRestResult(RetStatusNum.SYSTEM_ERROR.getCode(),RetStatusNum.SYSTEM_ERROR.getMessage(), (Object)null);
    }

    public static <T> ApiRestResult<T> err(CommonEnum commonEnum) {
        return new ApiRestResult(commonEnum.getResultCode(),commonEnum.getResultMsg(), null);
    }

    public static <T> ApiRestResult<T> err(T data) {
        return new ApiRestResult(RetStatusNum.SYSTEM_ERROR.getCode(), RetStatusNum.SYSTEM_ERROR.getMessage(), data);
    }

    public static <T> ApiRestResult<T> err(String msg, String code) {
        return new ApiRestResult(code, msg, (Object)null);
    }

    public String toString() {
        return "ApiRestResult{data=" + this.data + ", code='" + this.code + '\'' + ", msg='" + this.msg + '\'' + "} " + super.toString();
    }

    public static void main(String[] args) {
        String error = JSON.toJSONString(ApiRestResult.err("error"));
        System.out.println(error);
    }
}